/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.gen.entity;

import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.DataEntity;
import cn.net.ecode.common.persistence.annotation.Id;
import cn.net.ecode.common.utils.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;

/**
 * 生成表Entity
 * @author ThinkGem
 * @version 2014-9-9
 */
public class GenTable extends DataEntity<GenTable> {
	
	private static final long serialVersionUID = 1L;
	private String tableName; 		// 名称
	private String className;		// 实体类名称
	private String comments;		// 描述
	
	private String tplCategory;		// 生成模板分类
	private String packageName;		// 生成包路径
	private String moduleName;		// 生成模块名
	private String subModuleName;		// 生成子模块名
	private String functionName;		// 生成功能名
	private String functionNameSimple;	// 生成功能名（简写）
	private String functionAuthor;		// 生成功能作者
	private String genBaseDir;			// 生成的基础目录
	private String options;				// 其它生成选项

	private List<GenTableColumn> pkList; 	// 当前表主键列表
	private List<GenTableColumn> columnList = Lists.newArrayList();		// 表列
	private List<GenTableJoin> tableJoinList = Lists.newArrayList();	// 连接表

	private GenTable parent;	// 父表对象
	private List<GenTable> childList = Lists.newArrayList();	// 子表列表
	
	private String nameLike; 	// 按名称模糊查询
	private String flag; 		// 1：保存方案； 2：保存方案并生成代码
	private Boolean replaceFile;// 是否替换现有文件    0：不替换；1：替换文件
	
	public GenTable() {
		super();
//		this.tplCategory = "curd";
	}

	public GenTable(String id){
		super(id);
	}

	@Id
	@NotBlank(message="表名不能为空")
	@Length(min=0, max=64, message="表名长度不能超过 64 个字符")
	public String getTableName() {
		return StringUtils.lowerCase(tableName);
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public String getTplCategory() {
		return tplCategory;
	}

	public void setTplCategory(String tplCategory) {
		this.tplCategory = tplCategory;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getSubModuleName() {
		return subModuleName;
	}

	public void setSubModuleName(String subModuleName) {
		this.subModuleName = subModuleName;
	}

	public String getFunctionName() {
		return functionName;
	}

	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}

	public String getFunctionNameSimple() {
		return functionNameSimple;
	}

	public void setFunctionNameSimple(String functionNameSimple) {
		this.functionNameSimple = functionNameSimple;
	}

	public String getFunctionAuthor() {
		return functionAuthor;
	}

	public void setFunctionAuthor(String functionAuthor) {
		this.functionAuthor = functionAuthor;
	}

	public String getGenBaseDir() {
		return genBaseDir;
	}

	public void setGenBaseDir(String genBaseDir) {
		this.genBaseDir = genBaseDir;
	}

//	public String getParentTable() {
//		return StringUtils.lowerCase(parentTable);
//	}
//
//	public void setParentTable(String parentTable) {
//		this.parentTable = parentTable;
//	}
//
//	public String getParentTableFk() {
//		return StringUtils.lowerCase(parentTableFk);
//	}
//
//	public void setParentTableFk(String parentTableFk) {
//		this.parentTableFk = parentTableFk;
//	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public List<GenTableColumn> getPkList() {
		if (pkList == null){
			pkList = Lists.newArrayList();
			for (GenTableColumn c : columnList){
				if (Global.YES.equals(c.getIsPk())){
					pkList.add(c);
				}
			}
		}
		return pkList;
	}

	public void setPkList(List<GenTableColumn> pkList) {
		this.pkList = pkList;
	}

	public List<GenTableColumn> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<GenTableColumn> columnList) {
		this.columnList = columnList;
	}

	public List<GenTableJoin> getTableJoinList() {
		return tableJoinList;
	}

	public void setTableJoinList(List<GenTableJoin> tableJoinList) {
		this.tableJoinList = tableJoinList;
	}

	public GenTable getParent() {
		return parent;
	}

	public void setParent(GenTable parent) {
		this.parent = parent;
	}

	public List<GenTable> getChildList() {
		return childList;
	}

	public void setChildList(List<GenTable> childList) {
		this.childList = childList;
	}

	public String getNameLike() {
		return nameLike;
	}

	public void setNameLike(String nameLike) {
		this.nameLike = nameLike;
	}
	
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Boolean getReplaceFile() {
		return replaceFile;
	}

	public void setReplaceFile(Boolean replaceFile) {
		this.replaceFile = replaceFile;
	}

	@JsonIgnore
	public String getJdbcUsername() {
		return Global.getJdbcInfo("username");
	}

	/**
	 * 获取列名和说明
	 * @return
	 */
	public String getNameAndComments() {
		return getTableName() + (comments == null ? "" : "  :  " + comments);
	}

	/**
	 * 获取导入依赖包字符串
	 * @return
	 */
	public List<String> getImportList(){
		List<String> importList = Lists.newArrayList(); // 引用列表
		for (GenTableColumn column : getColumnList()){
			if (column.getIsNotBaseField() || ("1".equals(column.getIsQuery()) && "between".equals(column.getQueryType())
							&& ("createDate".equals(column.getSimpleJavaField()) || "updateDate".equals(column.getSimpleJavaField())))){
				// 导入类型依赖包， 如果类型中包含“.”，则需要导入引用。
				if (StringUtils.indexOf(column.getJavaType(), ".") != -1 && !importList.contains(column.getJavaType())){
					importList.add(column.getJavaType());
				}
			}
			if (column.getIsNotBaseField()){
				// 导入JSR303、Json等依赖包
				for (String ann : column.getAnnotationList()){
					if (!importList.contains(StringUtils.substringBefore(ann, "("))){
						importList.add(StringUtils.substringBefore(ann, "("));
					}
				}
			}
		}
		// 如果有子表，则需要导入List相关引用
		if (getChildList() != null && getChildList().size() > 0){
			if (!importList.contains("java.util.List")){
				importList.add("java.util.List");
			}
			if (!importList.contains("com.google.common.collect.Lists")){
				importList.add("com.google.common.collect.Lists");
			}
		}
		return importList;
	}
	
	/**
	 * 是否存在父类
	 * @return
	 */
	public Boolean getParentExists(){
		return parent != null && parent.getTableName() != null;// && StringUtils.isNotBlank(parentTable) && StringUtils.isNotBlank(parentTableFk);
	}

	/**
	 * 是否存在create_date列
	 * @return
	 */
	public Boolean getCreateDateExists(){
		for (GenTableColumn c : columnList){
			if ("create_date".equals(c.getColumnName())){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 是否存在update_date列
	 * @return
	 */
	public Boolean getUpdateDateExists(){
		for (GenTableColumn c : columnList){
			if ("update_date".equals(c.getColumnName())){
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否存在status列
	 * @return
	 */
	public Boolean getStatusExists(){
		for (GenTableColumn c : columnList){
			if ("status".equals(c.getColumnName())){
				return true;
			}
		}
		return false;
	}
}


